package com.hinnotek.warehouse.ui

import android.graphics.Rect
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.ImageViewInfo
import com.hinnotek.warehouse.bean.InstructionsBean
import com.hinnotek.warehouse.bean.WareHouseInfoBean
import com.hinnotek.warehouse.bean.WareHouseDetailBean
import com.hinnotek.warehouse.databinding.ActivityWarehouseDetailBinding
import com.hinnotek.warehouse.databinding.ItemQueryWarehouseDetailTypeLayoutBinding
import com.hinnotek.warehouse.viewmodel.WarehouseViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_WAREHOUSE_DETAILS
import com.xuexiang.xui.widget.imageview.preview.PreviewBuilder
import dagger.hilt.android.AndroidEntryPoint

@Route(path = ACTIVITY_WAREHOUSE_DETAILS)
@AndroidEntryPoint
class WarehouseDetailActivity : BaseActivity<ActivityWarehouseDetailBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<WareHouseDetailBean, ItemQueryWarehouseDetailTypeLayoutBinding>
    private val qrList by lazy { arrayListOf<ImageViewInfo>() }
    private val data = InstructionsBean.getData()
    
    private lateinit var layoutManager: LinearLayoutManager

    var wareData: WareHouseInfoBean? = null

    private val viewModel by viewModels<WarehouseViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_warehouse_detail)
        setTitle("盘库管理")
        bindingView.vm2 = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.autoRefresh()

    }

    override fun initVew() {

        adapter = SimpleBindingAdapter(
            R.layout.item_query_warehouse_detail_type_layout,
            ArrayList(),
            BR.detailData
        )
        layoutManager = LinearLayoutManager(this)
        bindingView.recycler.layoutManager = layoutManager
        bindingView.recycler.adapter = adapter
        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }

        wareData = intent.extras?.getSerializable("wareData") as WareHouseInfoBean
        viewModel.keyInt = wareData?.wareId ?: 0
        bindingView.wareTop = wareData

        adapter.addListener { binding, itemData ->

            binding.QRImage.setOnClickListener {

                PreviewBuilder.from(this@WarehouseDetailActivity)
                    .setImg(ImageViewInfo(itemData.productNumUrl))
                    .setSingleShowType(false)
                    .setProgressColor(R.color.xui_config_color_main_theme)
                    .start()

                /*computeBoundsBackward(layoutManager.findFirstVisibleItemPosition(), binding.QRImage)
                PreviewBuilder.from(this@WarehouseDetailActivity)
                    .setImgs(qrList)
                    .setCurrentIndex(adapter.getItemPosition(itemData))
                    .setSingleFling(true)
                    .setProgressColor(R.color.xui_config_color_main_theme)
                    .setType(PreviewBuilder.IndicatorType.Number)
                    .start()*/
            }

            /*binding.enclosure.setOnClickListener {
                *//*PreviewBuilder.from(this@WarehouseDetailActivity)
                    .setImg(ImageViewInfo(itemData.enUrl, Rect()))
                    .setFullscreen(true)
                    .setSingleShowType(false)
                    .setPhotoFragment(PhotoFragment::class.java)
                    .setProgressColor(R.color.xui_config_color_main_theme)
                    .setType(PreviewBuilder.IndicatorType.Number)
                    .start()*//*
            }*/
        }

        viewModel.autoRefresh()
    }

    /*private fun computeBoundsBackward(firstCompletelyVisiblePos: Int, view: ImageView) {
        qrList.clear()
        for (bean in adapter.data) {
            qrList.add(ImageViewInfo(bean.qrUrl))
        }
        for (i in firstCompletelyVisiblePos until adapter.itemCount) {
            val bounds = Rect()
            view.getGlobalVisibleRect(bounds)
            qrList[i].setBounds(bounds)
        }
    }*/

    override fun setData() {
        viewModel.warehouseDetailPage.observe(this, Observer {
            adapter.addAll(it, true)
        })

    }
}